package com.doget.data;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doget.data.bean.Topic;
import com.doget.data.delete.DeleteActivity;
import com.doget.data.dialog.PayMySelectDialog;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private PayMySelectDialog payDialog;
    private LinearLayout dialog;
    private int screenWidth;
    private int screenHeight;
    private RelativeLayout all;
    private TextView title;

    @Override
    protected void onDestroy() {
        Debug.stopMethodTracing();
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Debug.startMethodTracing();
        dialog = findViewById(R.id.dialog);
        all = findViewById(R.id.all);
        title = findViewById(R.id.title);
        test();
//        String api = BuildConfig.API;
//        String host = BuildConfig.HOST;
//        boolean debug = BuildConfig.DEBUG;
//
//        Toast.makeText(this, debug + "===" + api, Toast.LENGTH_LONG).show();
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyApplication.getContext(), DeleteActivity.class));
                test();
            }
        });
//
        // 屏幕宽（像素，如：480px）
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        // 屏幕高（像素，如：800p）
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        Log.e(" getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

        findViewById(R.id.textView1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test();
                //多个Activity嵌套时用this.parent否则异常

            }
        });
    }

    public void togetherRun(View view) {
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(dialog, "scaleX",
                1.0f, 2f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(dialog, "scaleY",
                1.0f, 2f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        //两个动画同时执行
        animSet.playTogether(anim1, anim2);
        animSet.start();
    }

    private void initLocation(TextView textView) {
        //获取抛物线原点坐标
        int[] point_location = new int[2];
        textView.getLocationInWindow(point_location);
        int pointX = point_location[0] - textView.getWidth() / 2;
        int pointY = point_location[1] - textView.getHeight() / 2;

        //获取抛物线起点坐标
        int startX = pointX - 500;
        int startY = pointY;

        //获取抛物线顶点坐标
        int topX = pointX;
        int topY = pointY - 800;

        //获取抛物线终点坐标
        int endX = pointX + 500;
        int endY = pointY;
    }

    /**
     * 抛物线 下降动画
     */

    public void playWithAfter(View view) {
        float allHeight = all.getHeight();
        float aaaa = (dialog.getRight() - dialog.getLeft()) / 2 + dialog.getLeft();
        Log.i("====", "playWithAfter: " + allHeight);
        Log.i("====", "playWithAfter: " + aaaa);
        Log.i("====", "playWithAfter: " + screenWidth);
        Log.i("====", "playWithAfter: " + dialog.getRight());
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);//沿着Y轴放大
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);//沿着X轴放大
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "translationX", 0.0f, screenWidth / 2);//沿着x轴平移
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(view, "translationY", 0.0f, allHeight / 2);//沿着Y轴平移

        ObjectAnimator animator22 = ObjectAnimator.ofFloat(view, "translationY", 0.0f, 350.0f, 0.0f);//沿着Y轴平移
        animator3.setInterpolator(new LinearInterpolator());
        animator4.setInterpolator(new AccelerateInterpolator());

//        animator2.setStartDelay(1000);
//        animator3.setStartDelay(2000);
//        animator4.setStartDelay(2000);
        /**
         * anim1，anim2,anim3同时执行
         * anim4接着执行
         */
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(animator1).with(animator2).with(animator3).with(animator4);
        animSet.setDuration(4000);
        animSet.start();
        animSet.addListener(animatorListener1);
    }

    public void propertyValuesHolder(View view) {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
    }


    private Animator.AnimatorListener animatorListener1 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
//            content.setVisibility(View.VISIBLE);//跳转进来时，（因为finish之前会将之设置为不可见，）
            // 根布局要设置为可见，与finish部分的不可见相对应
//            mPuppet.setAlpha(1);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
//            container.startAnimation(createAlphaAnimation());
//            container.setVisibility(View.INVISIBLE);//动画结束时，揭露动画设置为不可见
            dialog.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    };

    public static ArrayList arrayList = new ArrayList();

    //这儿发生泄漏
    public void test() {
        for (int i = 0; i < 1000; i++) {
            arrayList.add(new Topic("aa", i));
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

//    https://blog.csdn.net/a_zhon/article/details/89517374
}
